/**
 * created by 小卷毛, 2021-09-04
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.service

import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.IBinder

/**
 * 描 述：音频保活方案（无声音乐——不起作用）
 * 作 者：小卷毛  2021-09-04 11:56
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class PlayerMusicService : Service() {

    private lateinit var mMediaPlayer: MediaPlayer

    override fun onBind(intent: Intent?): IBinder? = null

    override fun onCreate() {
        super.onCreate()

        mMediaPlayer =
            MediaPlayer
                .create(
                    applicationContext,
                    -1 /*R.raw.silent*/
                )
                .apply { isLooping = true }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        startPlayMusic()
        return START_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()

        stopPlayMusic()
        stopForeground(true)
    }

    private fun startPlayMusic() {
        mMediaPlayer.start()
    }

    private fun stopPlayMusic() {
        mMediaPlayer.stop()
    }

}